bittorrent-tracker
Simple, robust, BitTorrent tracker (client & server) implementation
Node.js implementation of a BitTorrent tracker, client and server.
A BitTorrent tracker is a web service which responds to requests from BitTorrent
clients. The requests include metrics from clients that help the tracker keep overall
statistics about the torrent. The response includes a peer list that helps the client
participate in the torrent swarm.
This module is used by WebTorrent.
features
- Includes client & server implementations
- Supports all mainstream tracker types:
- Supports ipv4 & ipv6
- Supports tracker "scrape" extension
- Robust and well-tested
- Tracker statistics available via web interface at
/stats
or JSON data at /stats.json
Also see bittorrent-dht.
Tracker stats
install
npm install bittorrent-tracker
usage
client
To connect to a tracker, just do this:
import Client from 'bittorrent-tracker'
const requiredOpts = {
infoHash: new Buffer('012345678901234567890'),
peerId: new Buffer('01234567890123456789'),
announce: [],
port: 6881
}
const optionalOpts = {
rtcConfig: {},
userAgent: '',
wrtc: {},
getAnnounceOpts: function () {
return {
uploaded: 0,
downloaded: 0,
left: 0,
customParam: 'blah'
}
},
proxyOpts: {
socksProxy: new SocksProxy(socksOptionsObject),
httpAgent: new http.Agent(agentOptionsObject),
httpsAgent: new https.Agent(agentOptionsObject)
},
}
const client = new Client(requiredOpts)
client.on('error', function (err) {
console.log(err.message)
})
client.on('warning', function (err) {
console.log(err.message)
})
client.start()
client.on('update', function (data) {
console.log('got an announce response from tracker: ' + data.announce)
console.log('number of seeders in the swarm: ' + data.complete)
console.log('number of leechers in the swarm: ' + data.incomplete)
})
client.once('peer', function (addr) {
console.log('found a peer: ' + addr)
})
client.complete()
client.update()
client.update({
uploaded: 0,
downloaded: 0,
left: 0,
customParam: 'blah'
})
client.stop()
client.destroy()
client.scrape()
client.on('scrape', function (data) {
console.log('got a scrape response from tracker: ' + data.announce)
console.log('number of seeders in the swarm: ' + data.complete)
console.log('number of leechers in the swarm: ' + data.incomplete)
console.log('number of total downloads of this torrent: ' + data.downloaded)
})
server
To start a BitTorrent tracker server to track swarms of peers:
import { Server } from 'bittorrent-tracker'
const server = new Server({
udp: true,
http: true,
ws: true,
stats: true,
trustProxy: false,
filter: function (infoHash, params, cb) {
const allowed = (infoHash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa')
if (allowed) {
cb(null)
} else {
cb(new Error('disallowed torrent'))
}
}
})
server.http
server.udp
server.ws
server.on('error', function (err) {
console.log(err.message)
})
server.on('warning', function (err) {
console.log(err.message)
})
server.on('listening', function () {
const httpAddr = server.http.address()
const httpHost = httpAddr.address !== '::' ? httpAddr.address : 'localhost'
const httpPort = httpAddr.port
console.log(`HTTP tracker: http://${httpHost}:${httpPort}/announce`)
const udpAddr = server.udp.address()
const udpHost = udpAddr.address
const udpPort = udpAddr.port
console.log(`UDP tracker: udp://${udpHost}:${udpPort}`)
const wsAddr = server.ws.address()
const wsHost = wsAddr.address !== '::' ? wsAddr.address : 'localhost'
const wsPort = wsAddr.port
console.log(`WebSocket tracker: ws://${wsHost}:${wsPort}`)
})
const port = 0
const hostname = "localhost"
server.listen(port, hostname, () => {
})
server.on('start', function (addr) {
console.log('got start message from ' + addr)
})
server.on('complete', function (addr) {})
server.on('update', function (addr) {})
server.on('stop', function (addr) {})
Object.keys(server.torrents)
server.torrents[infoHash].complete
server.torrents[infoHash].incomplete
server.torrents[infoHash].peers
The http server will handle requests for the following paths: /announce
, /scrape
. Requests for other paths will not be handled.
multi scrape
Scraping multiple torrent info is possible with a static Client.scrape
method:
import Client from 'bittorrent-tracker'
Client.scrape({ announce: announceUrl, infoHash: [ infoHash1, infoHash2 ]}, function (err, results) {
results[infoHash1].announce
results[infoHash1].infoHash
results[infoHash1].complete
results[infoHash1].incomplete
results[infoHash1].downloaded
})
command line
Install bittorrent-tracker
globally:
$ npm install -g bittorrent-tracker
Easily start a tracker server:
$ bittorrent-tracker
http server listening on 8000
udp server listening on 8000
ws server listening on 8000
Lots of options:
$ bittorrent-tracker --help
bittorrent-tracker - Start a bittorrent tracker server
Usage:
bittorrent-tracker [OPTIONS]
If no --http, --udp, or --ws option is supplied, all tracker types will be started.
Options:
-p, --port [number] change the port [default: 8000]
--trust-proxy trust 'x-forwarded-for' header from reverse proxy
--interval client announce interval (ms) [default: 600000]
--http enable http server
--udp enable udp server
--ws enable websocket server
-q, --quiet only show error output
-s, --silent show no output
-v, --version print the current version
license
MIT. Copyright (c) Feross Aboukhadijeh and WebTorrent, LLC.